﻿using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;

namespace footwear
{
    public class CompanyInfoDao
    {
        public List<CompanyInfo> GetCompanyInfo()
        {
            try
            {
                DbConnect.CloseConnection();
            }
            catch { }
            DbConnect.OpenConnection();
            var list = new List<CompanyInfo>();
            try
            {
                var myCommand = new MySqlCommand("GetCompanyInfo;", DbConnect.Connection)
                                    {CommandType = CommandType.StoredProcedure};
                MySqlDataReader reader = myCommand.ExecuteReader();
                while (reader.Read())
                {
                    var companyInfo = new CompanyInfo
                                          {
                                              Name = reader["Name"].ToString(),
                                              NameEng = reader["NameEng"].ToString(),
                                              Address = reader["Address"].ToString(),
                                              AddressEng = reader["AddressEng"].ToString(),
                                              PhoneNumber = reader["PhoneNumber"].ToString()
                                          };
                    list.Add(companyInfo);
                }
                DbConnect.CloseConnection();
            }
            catch (MySqlException)
            {
                DbConnect.CloseConnection();
            }
            return list;
        }

        public bool AddCompanyInfo(string companyName, string companyPhoneNumber, string companyAddress)
        {
            try
            {
                DbConnect.CloseConnection();
            }
            catch { }
            DbConnect.OpenConnection();
            try
            {
                var myCommand = new MySqlCommand("AddCompanyInfo;", DbConnect.Connection) { CommandType = CommandType.StoredProcedure };
                myCommand.Parameters.AddWithValue("?name", companyName);
                myCommand.Parameters["?name"].Direction = ParameterDirection.Input;
                myCommand.Parameters.AddWithValue("?phoneNumber", companyPhoneNumber);
                myCommand.Parameters["?phoneNumber"].Direction = ParameterDirection.Input;
                myCommand.Parameters.AddWithValue("?address", companyAddress);
                myCommand.Parameters["?address"].Direction = ParameterDirection.Input;
                myCommand.ExecuteNonQuery();
                DbConnect.CloseConnection();
                return true;
            }
            catch (MySqlException)
            {
                DbConnect.CloseConnection();
                return false;
            }
        }
    }
}